
import com.quangtin3.Relay;
import java.util.LinkedList;
import java.util.List;
import org.apache.log4j.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * Xjrelay, relay in java.
 * @author quangtin3@gmai.com
 */
public class Xjrelay {

    private Logger log = Logger.getLogger("xjrelay");
    private List<Thread> threads = new LinkedList<Thread>();

    public Xjrelay(String[] args) {

        log.info("Xjrelay start with parameters");
        for (int i = 0; i < args.length; ++i) {
            log.info("param " + i + " : " + args[i]);
        }

        try {
            for (int i = 0; i < args.length; ++i) {
                RI ri = parse(args[i]);
                threads.add(new Thread(
                        new Relay(
                        ri.lport,
                        ri.caddr,
                        ri.cport,
                        Logger.getLogger(ri.name))));

            }
        } catch (IllegalArgumentException e) {
            log.info("relay parameters not correct, please check", e);
        }
    }

    /**
     * Start relay service.
     */
    public void start() {
        log.info("wrapper request start");
        for (Thread t : threads) {
            t.start();
        }
    }

    /**
     * Stop all relay service.
     */
    public void stop() {
        log.info("wrapper request stop");
        for (Thread t : threads) {
            t.stop();
        }
    }

    /**
     * Parse RI information from string. Valid value is:
     *  lport:caddr:cport
     *  caddr:cport
     * @param param string to parse
     * @return RI object
     */
    private static RI parse(String param) {
        String[] prs = param.split(":");
        try {
            if (prs.length == 4) {

                return new RI(prs[0], Integer.parseInt(prs[1]), prs[2],
                        Integer.parseInt(prs[3]));

            } else if (prs.length == 3) {

                return new RI(prs[0], prs[1], Integer.parseInt(prs[2]));
            }

        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Must in format name:lport:caddr:cport or caddr:cport", e);
        }

        throw new IllegalArgumentException("Must in format name:lport:caddr:cport or caddr:cport");
    }

    /**
     * Relay information
     */
    static class RI {

        /**
         * Relay information
         * @param name relay name
         * @param lport local port
         * @param caddr remote address
         * @param cport remote port
         */
        public RI(String name, int lport, String caddr, int cport) {
            this.name = name;
            this.lport = lport;
            this.caddr = caddr;
            this.cport = cport;
        }

        /**
         * Relay information
         * @param name relay name
         * @param caddr remote address
         * @param cport remote port
         */
        public RI(String name, String caddr, int cport) {
            this.name = name;
            this.caddr = caddr;
            this.cport = cport;
        }
        public int lport;   //local port
        public int cport;   //remote port
        public String caddr;//remote address
        public String name; //relay name
    }
}
